package com.example.tang.zhayan.fragment;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.tang.zhayan.R;
import com.example.tang.zhayan.adapter.HomeGirlItemAdapter;
import com.example.tang.zhayan.bean.BaseHomeGirl;
import com.example.tang.zhayan.bean.BaseSixTextJson;
import com.example.tang.zhayan.bean.HomeGirlItem;
import com.example.tang.zhayan.bean.HomeGirlList;
import com.example.tang.zhayan.bean.SixTextItem;
import com.example.tang.zhayan.bean.SixTextList;
import com.example.tang.zhayan.constant.PathConstant;
import com.example.tang.zhayan.ui.DesignerActivity;
import com.example.tang.zhayan.utils.FragmentUtils;
import com.example.tang.zhayan.utils.ResultParamsUtils;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.simple.eventbus.EventBus;
import org.xutils.common.Callback;
import org.xutils.common.util.LogUtil;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by tang on 2015/12/14.
 */
@ContentView(R.layout.fragment_home_page)
public class HomePageFragment extends BaseFragment implements View.OnClickListener {
    @ViewInject(R.id.ptr_home_page)
    private PullToRefreshListView ptrListview;
    private int num = 0;
    //获取系统的管理者
    private WindowManager manager;

    private TextView tv_left_top;
    private TextView tv_top;
    private TextView tv_right_top;
    private TextView tv_left_bottom;
    private TextView tv_bottom;
    private TextView tv_right_bottom;
    private TextView tv_center;

    private List<HomeGirlItem> homeGirlItemList = new ArrayList<>();
    private int page = 1;

    private HomeGirlItemAdapter adapter;

    private List<SixTextItem> sixTextItemList = new ArrayList<>();

    private EventBus bus = EventBus.getDefault();


    /**
     * 单例模式
     *
     * @return
     */
    public static HomePageFragment newInstance() {
        HomePageFragment fragment = new HomePageFragment();
        return fragment;

    }

    /**
     * 初始化控件 或者 数据
     * 比 initData() 方法先执行
     */
    @Override
    protected void initView() {
        super.initView();
        EventBus.getDefault().register(this);
        manager = (WindowManager) getActivity().getSystemService(getActivity().WINDOW_SERVICE);
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.home_page_head, null);
        final AbsListView.LayoutParams params = new AbsListView.LayoutParams(getScreenWidth(), getScreenHeight());
        view.setLayoutParams(params);

        initHeadView(view);


        ListView refreshableView = ptrListview.getRefreshableView();
        refreshableView.addHeaderView(view);
        adapter = new HomeGirlItemAdapter(getActivity(), homeGirlItemList);
        //pullToRefresh设置模式
        ptrListview.setMode(PullToRefreshBase.Mode.BOTH);
        refreshableView.setAdapter(adapter);
        refreshableView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position > 1) {
                    String memberId = homeGirlItemList.get(position - 2).getMemberId();
                    Intent intent = new Intent(getActivity(), DesignerActivity.class);
                    intent.putExtra("designerMemberId", memberId);
                    startActivity(intent);
                }
            }
        });
        //pullToRefresh  滑动事件监听
        ptrListview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                homeGirlItemList.clear();
                createListviewData(page);

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                page++;
                createListviewData(page);
            }
        });


    }

    /**
     * 初始化头部的控件，和设置监听
     *
     * @param view
     */
    private void initHeadView(View view) {
        tv_left_top = (TextView) view.findViewById(R.id.tv_left_top);
        tv_top = (TextView) view.findViewById(R.id.tv_top);
        tv_right_top = (TextView) view.findViewById(R.id.tv_right_top);
        tv_left_bottom = (TextView) view.findViewById(R.id.tv_left_bottom);
        tv_bottom = (TextView) view.findViewById(R.id.tv_bottom);
        tv_right_bottom = (TextView) view.findViewById(R.id.tv_right_bottom);
        tv_center = (TextView) view.findViewById(R.id.tv_center);

        tv_left_top.setOnClickListener(this);
        tv_top.setOnClickListener(this);
        tv_right_top.setOnClickListener(this);
        tv_left_bottom.setOnClickListener(this);
        tv_bottom.setOnClickListener(this);
        tv_right_bottom.setOnClickListener(this);
        tv_center.setOnClickListener(this);
    }

    /**
     * 获取屏幕的宽度
     */
    public int getScreenWidth() {
        Display display = manager.getDefaultDisplay();
        return display.getWidth();
    }

    /**
     * 获取屏幕的高度
     */
    public int getScreenHeight() {
        Display display = manager.getDefaultDisplay();
        return display.getHeight();
    }


    /**
     * 初始化控件 或者 数据
     */
    @Override
    protected void initData() {
        super.initData();
        createListviewData(page);
        createSixTextData();
    }


    /**
     * 创建六边形的数据
     */
    private void createSixTextData() {
        sixTextItemList.clear();
        Map<String, String> map = new HashMap<>();
        x.http().post(ResultParamsUtils.getIDParams(PathConstant.HOME_SIXTEXT_PATH, map), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                BaseSixTextJson json = JSONObject.parseObject(result, BaseSixTextJson.class);
                SixTextList data = json.getData();
                List<SixTextItem> list = data.getList();
                sixTextItemList.addAll(list);
                tv_left_top.setText(list.get(0).getTitle());
                tv_top.setText(list.get(1).getTitle());
                tv_right_top.setText(list.get(2).getTitle());
                tv_left_bottom.setText(list.get(3).getTitle());
                tv_bottom.setText(list.get(4).getTitle());
                tv_right_bottom.setText(list.get(5).getTitle());
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }

    /**
     * 创建listview的数据
     */
    private void createListviewData(int i) {
        RequestParams params = new RequestParams(PathConstant.HOME_LISTVIEW_PATH);
        params.addBodyParameter("pageSize", "10");
        params.addBodyParameter("pageNo", i + "");
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                BaseHomeGirl base = JSONObject.parseObject(result, BaseHomeGirl.class);
                HomeGirlList object = base.getData();
                homeGirlItemList.addAll(object.getList());
                adapter.notifyDataSetChanged();
                ptrListview.onRefreshComplete();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    /**
     * 六边形上的点击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_left_top:
                toSecondFragment(0);
                break;
            case R.id.tv_top:
                toSecondFragment(1);
                break;
            case R.id.tv_right_top:
                toSecondFragment(2);
                break;
            case R.id.tv_left_bottom:
                toSecondFragment(3);
                break;
            case R.id.tv_bottom:
                toSecondFragment(4);
                break;
            case R.id.tv_right_bottom:
                toSecondFragment(5);
                break;
            case R.id.tv_center:
                createSixTextData();
                break;
        }
    }


    public void toSecondFragment(int i) {
        if (sixTextItemList.size() != 0) {
            postId(sixTextItemList.get(i).getId());
        }
    }

    /**
     * 跳转到作品fragment
     */
    private void postId(String str) {
        EventBus.getDefault().post(str, "seek");
        EventBus.getDefault().post(str, "init");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
